import { useState } from 'react';
import { DEFAULT_RANGE } from 'src/constant/DateFilter';
// import { DateRange } from 'src/model/DateFilter';
import { calibrateBeginDate, calibrateEndDate } from 'src/util/datetimeHelper';

type DateRange = any

export const useDateFilter = () => {
  const [toDate, setToDate] = useState<Date>(calibrateEndDate(new Date()));
  const [fromDate, setFromDate] = useState<Date>(
    calibrateBeginDate(new Date(toDate.getTime() - DEFAULT_RANGE)),
  );

  const setDateRange = (dateRange: DateRange) => {
    dateRange.from && setFromDate(calibrateBeginDate(dateRange.from));
    dateRange.to && setToDate(calibrateEndDate(dateRange.to));
  };

  return {
    begin: fromDate.toISOString(),
    end: toDate.toISOString(),
    dateFilterProps: {
      fromDate,
      toDate,
      setDateRange,
    },
  };
};
